<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改)  工序节点物料表-->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="工艺路线Id" prop="processRouteId">
          <el-input v-model="formData.processRouteId" placeholder="请输入工艺路线Id"/>
        </el-form-item>
        <el-form-item label="工序节点表Id" prop="processRouteProcessNodeId">
          <el-input v-model="formData.processRouteProcessNodeId" placeholder="请输入工序节点表Id"/>
        </el-form-item>
        <el-form-item label="物料类型 0-投入1-产出" prop="materialType">
          <el-select v-model="formData.materialType" placeholder="请选择物料类型 0-投入1-产出">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="物料Id" prop="materialId">
          <el-input v-model="formData.materialId" placeholder="请输入物料Id"/>
        </el-form-item>
        <el-form-item label="物料编码" prop="materialNo">
          <el-input v-model="formData.materialNo" placeholder="请输入物料编码"/>
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="formData.materialName" placeholder="请输入物料名称"/>
        </el-form-item>
        <el-form-item label="转化率" prop="convertedRate">
          <el-input v-model="formData.convertedRate" placeholder="请输入转化率"/>
        </el-form-item>
        <el-form-item label="是否为主物料" prop="isMaster">
          <el-radio-group v-model="formData.isMaster">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序字段" prop="sort">
          <el-input v-model="formData.sort" placeholder="请输入排序字段"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as ProcessRouteProcessNodeMaterialApi from '@/api/basic/processrouteprocessnodematerial';

export default {
  name: "ProcessRouteProcessNodeMaterialForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        processRouteId: undefined,
        processRouteProcessNodeId: undefined,
        materialType: undefined,
        materialId: undefined,
        materialNo: undefined,
        materialName: undefined,
        convertedRate: undefined,
        isMaster: undefined,
        sort: undefined,
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await ProcessRouteProcessNodeMaterialApi.getProcessRouteProcessNodeMaterial(id);
          this.formData = res.data;
          this.title = "修改工序节点物料";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增工序节点物料";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await ProcessRouteProcessNodeMaterialApi.updateProcessRouteProcessNodeMaterial(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await ProcessRouteProcessNodeMaterialApi.createProcessRouteProcessNodeMaterial(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        processRouteId: undefined,
        processRouteProcessNodeId: undefined,
        materialType: undefined,
        materialId: undefined,
        materialNo: undefined,
        materialName: undefined,
        convertedRate: undefined,
        isMaster: undefined,
        sort: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
